package com.zxc.controller.marketing;

import com.zxc.entity.*;
import com.zxc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/execute")
public class ExecuteController {

    @Autowired
    private SalPlanService salPlanService;

    @Autowired
    private SalChanceService salChanceService;

    @Autowired
    private CstCustomerService cstCustomerService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private CstLinkmanService cstLinkmanService;

    @RequestMapping("/{skipCount}/{pageSize}/{chcId}")
    @ResponseBody
    public Map<String,Object> getSalChanceListOrByCustName(@PathVariable Integer skipCount,
                                                           @PathVariable Integer pageSize,
                                                           @PathVariable(value = "chcId")Integer chcId){
        Map<String,Object> modelMap = new HashMap<>();
        //计算出略过的记录数
        int skipCounts = (skipCount-1)*pageSize;
        List<SalPlan> salPlanList = salPlanService.getSalPlanListByCondition(skipCounts,pageSize,chcId);
        modelMap.put("dataList",salPlanList);
        int total = salPlanService.selectCountSalPlanForMarketing(chcId);
        modelMap.put("total",total);
        SalChance salChance = salChanceService.selectSalChanceBychcId(chcId);
        modelMap.put("salChance",salChance);
        return modelMap;
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String,Object> savePlan(Integer plaId,Integer plaChcId, String plaResult){
        SalPlan salPlan = new SalPlan();
        salPlan.setPlaResult(plaResult);
        salPlan.setPlaId(Long.valueOf(plaId));
        salPlan.setPlaChcId(Long.valueOf(plaChcId));
        Map<String,Object> modelMap = new HashMap<>();
        int  result = salPlanService.updateSalPlanPlaResult(salPlan);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String,Object> deletePlan(Integer[] plaId){
        Map<String,Object> modelMap = new HashMap<>();
        int result = salPlanService.deletePlans(plaId);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }

    @RequestMapping("/saveCustomer")
    @ResponseBody
    public Map<String,Object > save(CstCustomer cstCustomer, Integer chcId){
        Map<String,Object> modelMap = new HashMap<>();
        List<SysUser> sysUserList1 = sysUserService.selectSysUserBySysUserName(cstCustomer.getCustCreater());
        SysUser sysUser1 =  sysUserList1.get(0);
        cstCustomer.setCustCreater(sysUser1.getUserName());
        cstCustomer.setCustSatisfy(1);
        List<SysUser> sysUserList2= sysUserService.selectSysUserBySysUserName(cstCustomer.getCustManagerName());
        cstCustomer.setCustManagerId(sysUserList2.get(0).getUserId());
        cstCustomer.setCustStatus("1");
        cstCustomer.setCreateTime(new Date());

        int result = cstCustomerService.saveCstCustomer(cstCustomer);
        if (result > 0){
            modelMap.put("success",true);
            //1、正常 2、开发中 3、已归档-->开发成功、开发失败
            //修改对应的销售机会的状态为开发成功
            SalChance salChance = salChanceService.selectSalChanceBychcId(chcId);
            salChance.setChcStatus("3");
            int results = salChanceService.updateSalChanceByChcId(salChance);

            if (results <= 0){
                throw new RuntimeException("更新对应的销售机会信息失败！");
            }

            //在执行开发成功保存用户之前我们需要在客户联系人表里面添加数据
            //客户编号、客户名称、联系人手机号、联系人姓名
            CstLinkman cstLinkman = new CstLinkman();
            Long custNo = cstCustomerService.selectCstCustomerByCustName(salChance.getChcCustName());
            cstLinkman.setLkmCustNo(custNo);
            cstLinkman.setLkmCustName(salChance.getChcCustName());
            cstLinkman.setLkmTel(salChance.getChcTel());
            cstLinkman.setLkmName(salChance.getChcLinkman());
            int total = cstLinkmanService.saveCstLinkman(cstLinkman);
            if (total <= 0){
                throw new RuntimeException("添加联系人失败！");
            }
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }


    @RequestMapping(value = "/stopDevelop")
    @ResponseBody
    public Map<String,Object> stopDevelop(Integer chcId){
        Map<String,Object> modelMap = new HashMap<>();
        System.out.println("需要申请终止开发的记录chcId="+chcId);
        SalChance salChance = new SalChance();
        salChance.setChcId(Long.valueOf(chcId));
        salChance.setChcStatus("0");
        int result  = salChanceService.updateSalChanceByChcId(salChance);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }
}
